// pages/login/index.js
const util = require('../../utils/util.js');
const apiUrl = require('../../utils/apiUrl.js');
var http = require('../../utils/httpHelper.js');

//获取应用实例
const app = getApp();

Page({
  data: {
    userInfo: {},
    winWidth: 0,
    winHeight: 0,
    isLogin: false,
    isSaveLogin: false,
    username: '',
    password: '',
  },
  onLoad: function (options) {
    // 生命周期函数--监听页面加载
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    that.loadConfig();
    //获取用户信息
    http.ApiGet(apiUrl.UrlUserInfo, {}, function (res) {
      console.log(res);
      if (res&&res.code == 1) {
        that.setData({
          isLogin: false
        });
        // var user = wx.getStorageSync("user");
        var user = res.data.userinfo;
        util.setTabBarData(res.data);
        that.switchTab(user.is_reporter);
      } else {
        var username = wx.getStorageSync("username");
        var password = wx.getStorageSync("password");
        var isSaveLogin = true;
        if (!username) {
          username = '';
          isSaveLogin = false;
        }
        if (!password) {
          password = '';
        }

        that.setData({
          isLogin: true,
          username: username,
          password: password,
          isSaveLogin: isSaveLogin
        });
      }
    });
  },
  switchTab: function (is_reporter) {
    var that = this;
    that.doBinding();
    var user_role = wx.getStorageSync("user_role");

    if (is_reporter == 1) {
      if (!user_role) {
        wx.setStorageSync("user_role", "reporter");
      }

      that.loadReporterList();
      wx.reLaunch({
        url: '../task/index'
      });
    } else {
      if (!user_role) {
        wx.setStorageSync("user_role", "editor");
      }

      if(user_role=="reporter"){
        wx.reLaunch({
          url: '../task/index'
        });
      }else{
        wx.reLaunch({
          url: '../audit/index'
        });
      }
    }
  },
  formSubmit: function (e) {

    var that = this

    var formData = e.detail.value;

    console.log(formData);

    if (formData.username == "") {
      wx.showModal({
        title: '请填写用户名/手机号',
      });
      return;
    }

    if (formData.password == "") {
      wx.showModal({
        title: '请填写密码',
      });
      return;
    }

    wx.showToast({
      title: '正在提交',
      icon: 'loading',
      duration: 2000,
      mask: true,
    });

    http.ApiPost(apiUrl.UrlLogin, formData, function (res) {

      if (res.code == 1) {
        var user = res.data.userinfo;

        wx.showToast({
          title: '登录成功！',
          icon: 'success',
          duration: 5000
        })

        var isSaveLogin = that.data.isSaveLogin;
        if (isSaveLogin) {
          wx.setStorageSync("username", formData.username);
          wx.setStorageSync("password", formData.password);
        } else {
          wx.setStorageSync("username", "");
          wx.setStorageSync("password", "");
        }

        var keeplogin = res.data.keeplogin;
        wx.setStorageSync("user", user);

        if (user.is_reporter == 1) {
          wx.setStorageSync("user_role", "reporter");
        } else {
          wx.setStorageSync("user_role", "editor");
        }
        wx.setStorageSync("keeplogin", keeplogin);

        that.switchTab(user.is_reporter);

      } else {
        wx.showModal({
          title: res.msg
        });
      }
    });
  },
  loadConfig: function () {
    //获取配置
    http.ApiGet(apiUrl.UrlConfig, {}, function (res) {
      if (res&&res.code == 1) {
        wx.setStorageSync("easemobIM", res.data.easemob_im);
      } else {
        wx.setStorageSync("easemobIM", 0);
      }
    });
  },
  loadReporterList: function () {

    //获取同应用所有记者头像、昵称
    http.ApiGet(apiUrl.UrlReporterList, {}, function (res) {
      // console.log(res);
      if (res.code == 1) {
        wx.setStorageSync("reporterList", res.data);
      } else {
        wx.setStorageSync("reporterList", []);
      }
    });
  },
  doBinding: function () {
    var formData = {
      "platform": 4
    };

    var latitude = wx.getStorageSync("latitude");
    var longitude = wx.getStorageSync("longitude");
    var openId = wx.getStorageSync("openId");
    if (latitude) {
      formData["latitude"] = latitude;
    }
    if (longitude) {
      formData["longitude"] = longitude;
    }
    if (longitude) {
      formData["openid"] = openId;
    }

    http.ApiPost(apiUrl.UrlBinding, formData, function (res) {

      if (res.code == 1) {
        console.log(res.msg);
      }
    });
  },
  radioTap: function () {
    var isSaveLogin = this.data.isSaveLogin;
    if (isSaveLogin) {
      this.setData({
        isSaveLogin: false,
      });
    } else {
      this.setData({
        isSaveLogin: true,
      });
    }
  },
  linkTap: function () {
    wx.setClipboardData({
      data: "http://www.sogx.cn",
      success: function (res) {
        console.log(res);
      }
    });
  }
})